package com.pai4j.common.bean;

import com.pai4j.common.enums.ResponseCodeEnum;
import lombok.Data;

import java.util.List;

/**
 *  分页的统一响应
 *
 * @author: CYM-pai
 * @date: 2025/04/15 21:22
 **/

@Data
public class PAIPageResponseBeanUtil<T> {
    
    private Boolean success;
    
    private Integer code;
    
    private String message;
    
    private Long  pageNo;
    
    private Long pageSize;
    
    private Long total;
    
    private Long pageTotal;
    
    private List<T> data;
    
    /**
     * 默认构造函数
     *
     * @author: CYM-pai
     * @date: 2025/04/15 21:27
     **/
    public PAIPageResponseBeanUtil(){
    
    }
    
    /**
     *  构造分页成功的结果
     *
     * @author: CYM-pai
     * @date: 2025/04/15 21:27
     **/
    
    public static PAIPageResponseBeanUtil success(long pageNo, long pageSize, long total, List data){
        PAIPageResponseBeanUtil responseBean = new PAIPageResponseBeanUtil();
        responseBean.setSuccess(true);
        responseBean.setCode(ResponseCodeEnum.SUCCESS.getCode());
        responseBean.setMessage("OK");
        responseBean.setData(data);
        responseBean.setPageNo(pageNo);
        responseBean.setPageSize(pageSize);
        responseBean.setTotal(total);
        // 计算总页数，如果总记录数除以页面大小有余数，则总页数为总记录数除以页面大小加一，否则为总记录数除以页面大小
        responseBean.pageTotal = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        return responseBean;
    }
    
    /**
     * 构建分页失败的结果
     *
     * @author: CYM-pai
     * @date: 2025/04/15 21:34
     **/
    
    public static PAIPageResponseBeanUtil error(){
        PAIPageResponseBeanUtil responseBean = new PAIPageResponseBeanUtil();
        responseBean.setSuccess(false);
        responseBean.setCode(ResponseCodeEnum.ERROR.getCode());
        responseBean.setMessage("ERROR");
        return responseBean;
    }
}
